JavaServer

Servlet Tutorial


Documentation / Developer Docs / Administrator Docs / Index

The Java Server is both flexible and extensible. Using the Java Server APIs, you can write your own servlet and incorporate it into the server. To do this, follow these three steps:

  1. Write the servlet
  2. Configure the server
  3. Invoke the servlet

Writing the Servlet

Example of a servlet

import java.io.*;
import javax.servlet.*;

public class HelloServlet extends GenericServlet  {

  public void service(ServletRequest req, ServletResponse res) 
      throws ServletException, IOException {

    PrintStream out = new PrintStream(res.getOutputStream());
    out.println("Hello World!");
  }

  public String getServletInfo() {
    return "Hello World Servlet";
  }
}

After creating this source code in your favorite editor, compile this example servlet, and place the classfile in the server_root/servlets directory on your server. When compiling, don't forget to include the javax.servlet.* package in your classpath. The easiest way to do this is to include server_root/lib/classes.zip in your classpath. For instructions on how to do this, please refer to your compiler's documentation.

In the preceding code, the service method is called every time the servlet is accessed. It writes "Hello world!" to the response, which causes the phrase "Hello World!" to appear on the display of the browser accessing the servlet.

Configuring the Server

The real beauty of the Java Server is that it is extensible. But, before you can use a servlet to add extended functionality to the Java Server, you have to use the Java Server Administration applet to install the servlet and specify the default parameters and arguments.

Display the Administration Applet by connecting to:

For instructions on logging in and using the Administration Applet, see the Administration Log In/Log Out page.

The following instructions assume that you have already logged in, and are using the Servlet Loading facility to add a servlet. To do this, click on the Servlets button, and select Add from the list of choices on the left.

Then, to add a new servlet, fill in the following fields:

Example

To add the HelloServlet.class file located in the servlets directory, enter hello for the servlet name, and HelloServlet for the servlet class. Click on the Add button.

Invoking the Servlet

To invoke a servlet you call it by creating a URL with "/servlet/" prepended to the servlet name. Then enter this URL in your favorite browser to see the output of the servlet.

Example

After installing it according to the directions above, access the HelloServlet by entering the following URL in your favorite browser:

Mapping Servlets to URLs

Once you've added the servlet, you can also alias a servlet to a URL. This is done via the Servlet Aliases screen, under the Setup Button. To alias a servlet:

Example

To then map the HelloServlet to a URL, go to the Setup Section, and select Servlet Aliases. In the servlet aliases screen, click on the Add button. Then enter /hello.txt in the URL Pathname, and hello in the Servlet invoked field. Click on save.

The HelloServlet can now be invoked with the URL:

Server Side Includes

Server side include files (files with a .shtml extension), are parsed by the server, which invokes any servlets referenced in the HTML. The results are returned from the servlet are embedded into the HTML file, which is the returned to the requestor.

The syntax for the servlet tag is:

<servlet name=ServletName code=ServletCode.class 
initParam1=initArg1 initParam2=initArg2 ...>
<param name=param1 value=val1>
<param name=param2 value=val2>
.
.
.
</servlet>

The server first tries to invoke the servlet with the name referenced by the name field. If this fails or if no name is provided it attempts to load the servlet based on the code field, passing any remaining fields within the <servlet> tag to the servlet as initialization arguments. If the server loads the servlet and a name was specified, the server keeps the servlet loaded so that the next time the servlet is accessed it is not reloaded. If no name is specified, the server reloads the servlet each time it is accessed.

Once the server has a handle to the servlet, either by referencing it from the name or loading it, it calls service on the servlet. The name value pairs specified in the html file with the <param> tag are accessible to the servlet using the standard getParameter and getParameters methods on the ServletRequest object passed to the servlet in service.

Everything the servlet writes to ServletResponse.getOutputStream() gets written out to the client as part of the document the client requested. The servlet's output appears in the document at the location where the servlet tag was embedded.

Example

In your favorite browser, create the following html as the file server_root/public_html/hello_ssi.shtml

<HTML><HEAD><TITLE>Hello World SSI</TITLE></HEAD>
<BODY>
<H1>Amazing Server Side Include Text Follows:</H1>
<servlet name=hello>
</servlet>
</BODY></HTML>


Top
java-server-feedback@java.sun.com